#include <iostream>
#include <vector>
using namespace std;

// 折半查找
int BSearch(vector<int> vec, int num)
{
    int low = 0, high = vec.size() - 1, mid;
    while (low <= high)
    {
        mid = (low + high) >> 1;
        if (vec[mid] == num)
        {
            return mid;
        }
        else if (vec[mid] < num)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return high; // high+1为应该插入的位置
}

int Solution(vector<int> vec, int num)
{
    int i = BSearch(vec, num);
    return i == -1 ? -1 : vec[i];
}

int main()
{
    vector<int> vec = {1, 2, 3, 4, 6};

    cout << Solution(vec, 0) << endl;
    cout << Solution(vec, 5) << endl;
    cout << Solution(vec, 1) << endl;
    cout << Solution(vec, 3) << endl;

    return 0;
}